<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <div class="u-flex">
                <div>
                    <button class="layui-btn" id="btn-addmenu">添加菜单</button>
                </div>
                <div class="layui-btn-group u-pl-5">
                    <button class="layui-btn" id="btn-expand">全部展开</button>
                    <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
                </div>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add">新增</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        // 渲染表格
        var renderTable = function () {
            layer.load(2);
            treetable.render({
                treeColIndex: 1,
                treeSpid: '0',
                treeIdName: 'id',
                treePidName: 'pid',
                elem: '#munu-table',
                url: '/menu/menuTreeData',
                page: false,
                cols: [[
                    {type: 'numbers'},
                    {field: 'title', minWidth: 200, title: '权限名称'},
                    // {field: 'authority', title: '权限标识'},
                    {field: 'url', title: '菜单url'},
                    {field: 'orderNum', width: 80, align: 'center', title: '排序号'},
                    {
                        field: 'pid', width: 80, align: 'center', templet: function (d) {
                            if (d.pid == 0) {
                                return '<span class="layui-badge layui-bg-blue">目录</span>';
                            } else {
                                return '<span class="layui-badge-rim">菜单</span>';
                            }
                        }, title: '类型'
                    },
                    {field: 'visible', width: 80, align: 'center',templet:function(e){
                            if (e.visible == 0) {
                                return '<div style="color: #00ba00">启用<div/>'
                            } else {
                                return '<div style="color: red">停用<div/>'
                            }
                        },title: '状态'},
                    {templet: '#auth-state', width: 180, align: 'center', title: '操作'}
                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });
        }
        renderTable();

        $('#btn-addmenu').click(function () {
            let index = layer.open({
                title: '添加菜单',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: './menu-add.html?parentId=0',
                end : function() {
                    //关闭弹框后触发
                    renderTable();
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if(layEvent === 'add'){
                let index = layer.open({
                    title: '添加菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: './menu-add.html?parentId='+data.id,
                    end : function() {
                        //关闭弹框后触发
                        renderTable();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }else if (layEvent === 'del') {
                layer.confirm('确定删除' + data.title + '吗', function (index) {
                    $.ajax({
                        type: "get",
                        url: '/menu/remove/'+data.id,
                        contentType: 'application/json',
                        success: function (req) {
                            if (req.code == 200) {
                                layer.msg("已删除");
                                renderTable();
                            } else {
                                layer.msg("删除失败");
                            }
                            layer.close(index);
                        },
                        error: function (e) {
                            layer.msg("删除失败");
                            layer.close(index);
                        }
                    })
                });
            } else if (layEvent === 'edit') {
                let index = layer.open({
                    title: '修改菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: './menu-edit.html?id='+data.id+'&parentId='+data.pid,
                    end : function() {
                        //关闭弹框后触发
                        renderTable();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
        });
    });

</script>
</body>
</html>
